package grape.attend.rest.vacation.mvc;

import com.baomidou.mybatisplus.core.metadata.IPage;
import grape.attend.rest.vacation.form.VacationListForm;
import grape.common.exception.runtime.RBaseException;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.*;
import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import javax.validation.Valid;
import grape.attend.rest.vacation.form.VacationCreateForm;
import grape.attend.rest.vacation.form.VacationUpdateForm;
import grape.attend.rest.vacation.form.VacationListPageForm;
import grape.attend.rest.vacation.vo.VacationVo;
import grape.attend.rest.vacation.mapper.VacationWebMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RestController;
import grape.common.rest.mvc.BaseController;
import grape.attend.service.vacation.po.Vacation;
import grape.attend.service.vacation.api.IVacationService;
import java.util.List;
/**
 * <p>
 * 假期表 前端控制器
 * </p>
 *
 * @author yangwei
 * @since 2020-05-08
 */
@RestController
@RequestMapping("/attend/vacation")
@Api(tags = "假期表相关接口")
public class VacationController extends BaseController<VacationVo, Vacation> {

    @Autowired
    private VacationWebMapper currentWebMapper;
    @Autowired
    private IVacationService currentService;



     @ApiOperation("添加假期表")
     @PreAuthorize("hasAuthority('vacation:single:create')")
     @PostMapping
     @ResponseStatus(HttpStatus.CREATED)
     public VacationVo create(@RequestBody @Valid VacationCreateForm cf) {
         // code 唯一检查
         if (currentService.existCode(cf.getCode())) {
             throw new RBaseException("编码已存在");
         }
         Vacation po = currentWebMapper.formToPo(cf);
         return super.create(po);
     }

     @ApiOperation("根据ID查询假期表")
     @PreAuthorize("hasAuthority('vacation:single:queryById')")
     @GetMapping("/{id}")
     @ResponseStatus(HttpStatus.OK)
     public VacationVo queryById(@PathVariable String id) {
         return super.queryById(id);
     }

     @ApiOperation("删除假期表")
     @PreAuthorize("hasAuthority('vacation:single:deleteById')")
     @DeleteMapping("/{id}")
     @ResponseStatus(HttpStatus.NO_CONTENT)
     public boolean deleteById(@PathVariable String id) {
         return super.deleteById(id);
     }

     @ApiOperation("更新假期表")
     @PreAuthorize("hasAuthority('vacation:single:updateById')")
     @PutMapping("/{id}")
     @ResponseStatus(HttpStatus.CREATED)
     public VacationVo update(@PathVariable String id,@RequestBody @Valid VacationUpdateForm uf) {
         Vacation po = currentWebMapper.formToPo(uf);
         po.setId(id);
         return super.update(po);
     }

    @ApiOperation("不分页查询假期表")
    @PreAuthorize("hasAuthority('vacation:single:list')")
    @GetMapping("/list")
    @ResponseStatus(HttpStatus.OK)
    public List<VacationVo> list(VacationListForm listForm) {
        Vacation po = currentWebMapper.formToPo(listForm);
        return super.list(po);
    }

    @ApiOperation("分页查询假期表")
    @PreAuthorize("hasAuthority('vacation:single:listPage')")
    @GetMapping("/listPage")
    @ResponseStatus(HttpStatus.OK)
    public IPage<VacationVo> listPage(VacationListPageForm listPageForm) {
         Vacation po = currentWebMapper.formToPo(listPageForm);
         return super.listPage(po,listPageForm);
     }

}
